﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Report;
using B.S.BasicData.Read.DTOS;

using MediatR;
using Microsoft.EntityFrameworkCore;
using NLog.Time;

namespace B.S.BasicData.Read.Apilication.Handler.Report
{
    public class GetleaveDetailHandler : IRequestHandler<GetleaveDetailCommon, ApiResult<List<LeaveDateilDTO>>>
    {
       
        private readonly IBaseRepository<leaveModel> leaveRepo;
        private readonly IBaseRepository<UserModel> userRepo;

        public GetleaveDetailHandler(IBaseRepository<leaveModel> leaveRepo,IBaseRepository<UserModel> userRepo)
        {
          
            this.leaveRepo = leaveRepo;
            this.userRepo = userRepo;
        }

        public async Task<ApiResult<List<LeaveDateilDTO>>> Handle(GetleaveDetailCommon request, CancellationToken cancellationToken)
        {
            ApiResult<List<LeaveDateilDTO>> res = new ApiResult<List<LeaveDateilDTO>>();
            var users = userRepo.GetValue(request.UserId).Result;
            if (users==null)
            {
                res.Code = ApiEnum.Error;
                res.Message = "用户不存在";
                return await Task.FromResult(res);
            }
            var leaveRecords = leaveRepo.GetAll()
                .Where(x => x.UserId == request.UserId && x.leaveTime.Month == request.Month).Select(x=> new LeaveDateilDTO
                {
                    UserId = request.UserId,
                    leaveName = users.UserName,
                    Month = request.Month,
                    leaveTime=x.leaveTime,  
                    EndleaveTime=x.EndleaveTime,
                    Number = x.Number,
                    cause=x.cause,
                }).ToList();
            res.Data = leaveRecords;
            res.Message = "返回明细数据";
            res.Code=ApiEnum.Success;
            return await Task.FromResult(res);



        }
    }
}
